/**
 * Created with  SublimeText
 * @Author:      Lyle
 * @DateTime:    2017-05-27 10:45:58
 * @Description: 超个2以上局部公用模块都调用的业务逻辑代码可放入
 */


var globalMixin = {

    methods: {

        globalMixin_Test: function() {
            console.log('globalMixin_Test');
        },

        getAvatar: function(gender, img) {
            if (gender == 'undefined') {
                gender = 1;
            }
            if (img != '' && img != undefined) {
                img = img.indexOf('woman') > -1 || img.indexOf('man') > -1 || img.indexOf('couple') > -1 ? '' : img;
            }
            if (img == '' || img == undefined) {
                if (parseInt(gender) == 2 || parseInt(gender) == 32) {
                    img = require('../assets/icon_male.png');
                } else if (parseInt(gender) == 1 || parseInt(gender) == 256) {
                    img = require('../assets/icon_remale.png');
                } else if (parseInt(gender) == 16 || parseInt(gender) == 64) {
                    img = require('../assets/icon_married.png');
                }
            }
            return img;
        },

        getGenderBgClass: function(gender) {
            var genderClass;
            if (gender == 'undefined') {
                gender = 1;
            }
            if (parseInt(gender) == 2 || parseInt(gender) == 32) {
                genderClass = 'icon_male';
            } else if (parseInt(gender) == 1 || parseInt(gender) == 256) {
                genderClass = 'icon_remale';
            } else if (parseInt(gender) == 16 || parseInt(gender) == 64) {
                genderClass = 'icon_married';
            } else {
                genderClass = 'icon_default_avatar';
            }
            return genderClass;
        },

        filterGenderImg: function(img) {
            var imgSrc = img;
            if (img.indexOf('woman') > -1 || img.indexOf('man') > -1 || img.indexOf('couple') > -1) {
                imgSrc = '';
            }
            return imgSrc;
        },

        judgeNum: function(num) {
            return parseInt(num) > 99 ? '99+' : num;
        },

        setSiteNavBottom: function(key, value) {
            let newSiteNavBottom = {};
            Object.assign(newSiteNavBottom, this.$store.state.base.siteNavBottom);
            newSiteNavBottom[key] = value;
            this.$store.dispatch('updateSiteNavBottom', newSiteNavBottom);
        },

        gotoUserProfile(profId) {
            let myUsrId = this.$store.state.user.userInfo.account.usr_id;
            if (parseInt(myUsrId) != parseInt(profId)) {
                this.$router.push({
                    name: 'userProfile',
                    query: {
                        userId: profId
                    }
                });
            } else {
                this.$router.push({
                    name: 'me',
                });
            }
        },

        formatUserLocation: function(obj) {
            /*
                obj={
                    age: '',
                    gender: '',
                    city: '',
                    state: '',
                    country: '',
                }
            */
            let str = '',
                locationArr = [],
                checkParameter = function(parameter) {
                    if (typeof(parameter) != 'undefined' && parameter != null && _.trim(parameter).length > 0) {
                        return true
                    } else {
                        return false;
                    }
                };
            if (checkParameter(obj.state) || checkParameter(obj.city)) {
                delete obj.country;
            };
            for (let i in obj) {
                if (obj.hasOwnProperty(i) && checkParameter(obj[i])) {
                    locationArr.push(obj[i]);
                }
            };
            str = checkParameter(obj.age) ? locationArr.join(', ').replace(', ', ' ● ') : locationArr.join(', ');
            return str;
        },

    }
}

export default globalMixin;
